

#include <iostream>
#include "x_execall.h"

char *env[] = {
"CONTENT_LENGTH=0",
"CONTENT_TYPE=application/x-www-form-urlencoded",
"DOCUMENT_ROOT=/pubwww",
"QUERY_STRING=op=ini&d=12345",
"REDIRECT_STATUS=200",
"REQUEST_METHOD=GET",
"REQUEST_URI=/inbox/phptest.php",
"SCRIPT_FILENAME=/home/sauvag_r/Desktop/zia/trunk/ModulePHP/phptest.php",//le chemin du script
"SCRIPT_NAME=/inbox/phptest.php",
"SERVER_NAME=localhost",
"SERVER_PROTOCOL=HTTP/1.0",
"SERVER_SOFTWARE=nimp",
"SystemRoot=C:/WINDOWS",
};

char *arg[] = {
  "/usr/bin/php-cgi",
  NULL
};

extern "C"
{
#ifdef _WIN32
__declspec(dllexport) int __cdecl module_setup(void)
{
	return (0);
}

int module_callback(void)
{
	int err;
	int old;
	int old2;
	FILE *fd;

	if (fopen_s(&fd, "temp", "w") == 0)
	{
		old = _dup(1);
		old2 = _dup(2);
		_dup2(_fileno(fd), 1);
		_dup2(_fileno(fd), 2);
		err = _spawnve(P_WAIT, "C:/Users/patate/Downloads/php-5.3.5/php-cgi.exe", arg, env);
		_dup2(old, 1);
		_dup2(old2, 2);
		fclose(fd);
	}
	return (0);
}

#else

int module_setup(void)
{
	return (0);
}

int module_callback(void)
{
	int err;
	int status;
	int old;
	int old2;
	int fd;
	int pid;

	err = 0;
	pid = fork();
    if (pid == 0)
	{
		if ((fd = open("temp", O_WRONLY | O_CREAT, S_IRUSR)) > 0)
		{
			old = dup(1);
			old2 = dup(2);
			dup2(fd, 1);
			dup2(fd, 2);
			err = execve("/usr/bin/php-cgi", arg, env);
			dup2(old, 1);
			dup2(old2, 2);
			close(fd);
		}
		else
			std::cerr << "File error." << std::endl;
	}
	else if (pid > 0)
		wait(&status);
	else
		err = pid;
	if (err)
		std::cerr << "An error occured." << std::endl;
	return (0);
}
#endif

}

